﻿using G19Glower.Shared;
using System.Linq;

namespace G19Glower.App
{
    internal class GlowerDetails
    {
        private IGlower mInstance;
        private string mTitle;
        private string mDescription;

        internal GlowerDetails(IGlower glower)
        {
            mInstance = glower;

            var glowerInfo = (GlowerInfoAttribute)glower.GetType().GetCustomAttributes(typeof(GlowerInfoAttribute), true).FirstOrDefault();

            if (glowerInfo == null)
            {
                mTitle = glower.GetType().Name;
                mDescription = glower.GetType().FullName;
            }
            else
            {
                mTitle = glowerInfo.Title;
                mDescription = glowerInfo.Description;
            }
        }

        public IGlower Instance
        {
            get
            {
                return mInstance;
            }
        }

        public string Title
        {
            get
            {
                return mTitle;
            }
        }

        public string Description
        {
            get
            {
                return mDescription;
            }
        }

        public bool DesiredRunningState
        {
            get;
            set;
        }

        internal void ApplyDesiredRunningState()
        {
            if (mInstance.IsRunning != this.DesiredRunningState)
            {
                if (this.DesiredRunningState)
                {
                    mInstance.Start();
                }
                else
                {
                    mInstance.Stop();
                }
            }
        }
    }
}
